package com.seek.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seek.commonutils.Statistics;
import com.seek.eduservice.entity.EduCourse;
import com.seek.eduservice.entity.vo.CoursePublishVo;
import com.seek.eduservice.entity.vo.CourseQuery;
import com.seek.eduservice.entity.vo.front.CourseInfoVo;
import com.seek.eduservice.entity.vo.front.CourseVo;
import com.seek.eduservice.mapper.EduCourseMapper;
import com.seek.eduservice.service.EduChapterService;
import com.seek.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seek.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author wml
 * @since 2022-01-16
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduVideoService eduVideoService;

    // 根据课程 id 查询课程确认信息
    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        return baseMapper.getPublishCourseInfo(courseId);
    }

    // 删除课程
    @Override
    public void removeCourse(String courseId) {
        // 1. 根据课程 id 删除小节
        eduVideoService.removeByCourseId(courseId);
        // 2. 根据课程 id 删除章节
        eduChapterService.removeByCourseId(courseId);
        // 3. 根据课程 id 删除课程
        baseMapper.deleteById(courseId);
    }

    // 根据条件查询课程信息带分页 查列表数据
    @Override
    public List<CourseQuery> getPageCondition(CourseQuery courseQuery) {
        List<CourseQuery> records = baseMapper.getPageCondition(courseQuery);
        return records;
    }

    // 根据条件查询课程信息带分页 查总数
    @Override
    public int count(CourseQuery courseQuery) {
        return baseMapper.getCountByCourseQuery(courseQuery);
    }

    // 条件查询带分页查询课程
    @Override
    public Map<String, Object> getFrontCourseList(Page<EduCourse> page, CourseVo courseVo) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        // 判断条件是否为空，不为空则拼接 sql 语句
//        if (courseVo.title != null && !courseVo.title.equals("")) {}
//        if (courseVo.teacherId != null && !courseVo.teacherId.equals("")) {}
        // 一级分类
        if (courseVo.subjectParentId != null && !courseVo.subjectParentId.equals("")) {
            queryWrapper.eq("subject_parent_id", courseVo.subjectParentId);
        }
        // 二级分类
        if (courseVo.subjectId != null && !courseVo.subjectId.equals("")) {
            queryWrapper.eq("subject_id", courseVo.subjectId);
        }
        // 根据销量排序
        if (courseVo.buyCountSort != null && !courseVo.buyCountSort.equals("")) {
            queryWrapper.orderByDesc("buy_count");
        }
        // 根据时间排序
        if (courseVo.gmtCreateSort != null && !courseVo.gmtCreateSort.equals("")) {
            queryWrapper.orderByDesc("gmt_create");
        }
        // 根据价格排序
        if (courseVo.priceSort != null && !courseVo.priceSort.equals("")) {
            queryWrapper.orderByDesc("price");
        }
        Page<EduCourse> eduCoursePage = baseMapper.selectPage(page, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("items", eduCoursePage.getRecords());
        map.put("current", eduCoursePage.getCurrent());
        map.put("pages", eduCoursePage.getPages());
        map.put("size", eduCoursePage.getSize());
        map.put("total", eduCoursePage.getTotal());
        map.put("hasNext", eduCoursePage.hasNext());
        map.put("hasPrevious", eduCoursePage.hasPrevious());
        return map;
    }

    // 根据课程 id 查询课程信息
    @Override
    public CourseInfoVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }

    // 根据浏览量降序查询十条数据
    @Override
    public Map<String, Object> getPopularCourse() {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("title", "view_count");
        queryWrapper.orderByDesc("view_count");
        queryWrapper.last("limit 0, 10");
        List<EduCourse> list = baseMapper.selectList(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        List<String> xAxis = new ArrayList<>();
        List<Integer> series = new ArrayList<>();
        for (EduCourse eduCourse : list) {
            xAxis.add(eduCourse.getTitle());
            series.add(eduCourse.getViewCount().intValue());
        }
        map.put("xAxis", xAxis);
        map.put("series", series);
        return map;
    }
}
